package health.lm.com.mine.pop.agreement;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;

import health.lm.com.component_base.widget.CustomPopWindow;
import health.lm.com.mine.R;
import health.lm.com.mine.entity.ServiceEntity;
import health.lm.com.mine.model.MineModel;
import health.lm.com.network.callback.SimpleCallBack;
import health.lm.com.router.ARouterConstant;

/**
 * 服务协议
 */
public class AgreeMentPopUtil {
    public static AgreeMentPopUtil upLevelPopUtil;
    private CustomPopWindow popWindow;
    private ServiceEntity bean;

    private AgreeMentPopUtil() {
    }

    public static AgreeMentPopUtil getInstance(){
        if (null == upLevelPopUtil) {
            upLevelPopUtil = new AgreeMentPopUtil();
        }
        return upLevelPopUtil;
    }

    /**
     * 显示popwindow
     */
    public void init(Context context, View mLayoutPopup, String title) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_agreement, null);
//        //主布局
        TextView tvTitle = contentView.findViewById(R.id.tv_title);
        WebView webView = contentView.findViewById(R.id.wb_content);
        webView.setBackgroundColor(context.getResources().getColor(R.color.color_f8f8f8));
        //确定
        TextView tv_confirm = contentView.findViewById(R.id.tv_confirm);
         contentView.findViewById(R.id.rl_main).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 popWindow.dissmiss();
             }
         });
        tv_confirm.setOnClickListener(v ->{
            if(null != bean) {
                ARouter.getInstance().build(ARouterConstant.ActivationActivity).withString(ARouterConstant.ACTIVATE_FEE, bean.getActive_fee())
                        .navigation();
                popWindow.dissmiss();
            }
        });
        MineModel.getInstance().service(new SimpleCallBack<ServiceEntity>() {
            @Override
            public void onSuccess(ServiceEntity entity) {
                bean = entity;
                webView.loadDataWithBaseURL( null, entity.getTreaty() , "text/html", "UTF-8", null ) ;
                popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                        .setView(contentView)
                        .enableBackgroundDark(true)
                        .setBgDarkAlpha(0.3f)
                        .setAnimationStyle(R.style.pop_bottom_anim)
                        .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
                        .create()
                        .showAtLocation(mLayoutPopup, Gravity.CENTER, 0, 0);
            }

        });
    }
}
